/*
 *    Copyright (c) 2018-2025, cloud All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: cloud
 */

package com.fuyao.cloud.asset.contract.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fuyao.cloud.asset.dto.contract.AssetContractDTO;
import com.fuyao.cloud.asset.entity.AssetContract;
import com.fuyao.cloud.common.core.util.R;
import com.fuyao.cloud.common.data.mybatis.SpliceQueryWrapper;
import com.fuyao.cloud.common.log.annotation.SysLog;
import com.fuyao.cloud.asset.contract.service.AssetContractService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.fuyao.cloud.common.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

/**
 * 合同管理
 *
 * @author liyue
 * @date 2021-11-18 18:45:12
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/fyassetcontract")
@Api(value = "fyassetcontract", tags = "合同管理管理")
public class AssetContractController {

	private final AssetContractService fyAssetContractService;

	/**
	 * 分页查询
	 *
	 * @param page            分页对象
	 * @param assetContractDTO 合同管理
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('contract_fyassetcontract_view')")
	public R getFyAssetContractPage(Page page, AssetContractDTO assetContractDTO) {
		return R.ok(fyAssetContractService.page(page, SpliceQueryWrapper.spliceSearchCondition(assetContractDTO)));
	}


	/**
	 * 通过id查询合同管理
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('contract_fyassetcontract_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(fyAssetContractService.getById(id));
	}

	/**
	 * 新增合同管理
	 *
	 * @param fyAssetContract 合同管理
	 * @return R
	 */
	@ApiOperation(value = "新增合同管理", notes = "新增合同管理")
	@SysLog("新增合同管理")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('contract_fyassetcontract_add')")
	public R save(@RequestBody AssetContract fyAssetContract) {
		return R.ok(fyAssetContractService.save(fyAssetContract));
	}

	/**
	 * 修改合同管理
	 *
	 * @param fyAssetContract 合同管理
	 * @return R
	 */
	@ApiOperation(value = "修改合同管理", notes = "修改合同管理")
	@SysLog("修改合同管理")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('contract_fyassetcontract_edit')")
	public R updateById(@RequestBody AssetContract fyAssetContract) {
		return R.ok(fyAssetContractService.updateById(fyAssetContract));
	}

	/**
	 * 通过id删除合同管理
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除合同管理", notes = "通过id删除合同管理")
	@SysLog("通过id删除合同管理")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('contract_fyassetcontract_del')")
	public R removeById(@PathVariable Long id) {
		return R.ok(fyAssetContractService.removeById(id));
	}


	/**
	 * 导出excel 表格
	 *
	 * @param assetContractDTO 查询条件
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('contract_fyassetcontract_export')")
	public List export(AssetContractDTO assetContractDTO,String type) {
		return fyAssetContractService.listContract(SpliceQueryWrapper.spliceSearchCondition(assetContractDTO), type);
	}

	/**
	 * 返回资产分类树结构
	 *
	 * @return 树集合
	 */
	@GetMapping(value = "/tree")
	public R getYearTree() {
		return R.ok(fyAssetContractService.selectTree());
	}

	/**
	 * 查询同合树
	 *
	 * @return 合同树结构
	 */
	@GetMapping(value = "/contractTree")
	public R getContractTree(AssetContract contract) {
		return R.ok(fyAssetContractService.selectContractTree(Wrappers.query(contract)));
	}

	@PostMapping(value = "/create/{type}")
	public R create(@PathVariable String type) {
		AssetContract assetContract = AssetContract.builder()
				.status("0")
				.type(type)
				.unid(UUID.randomUUID().toString())
				.build();
		return R.ok(assetContract);
	}

}
